package com.danskin.wechat.webapi;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.DanskinSMSApiClient;
import com.danskin.wechat.entity.CrmBrandInfo;
import com.danskin.wechat.entity.WxFansInfo;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CrmBrandInfoService;
import com.danskin.wechat.service.CustInfoService;
import com.danskin.wechat.util.StringUtil;

@RestController
@RequestMapping("api/system")
public class SystemController extends BaseApiController {
	@Autowired
	private CrmBrandInfoService crmBrandInfoService;

	@Autowired
	private CustInfoService custInfoService;

	@Autowired
	private TaskExecutor executor;

	@RequestMapping(value = "sendValidCode/{appId}", method = RequestMethod.POST)
	public Object sendValidCode(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId)
			throws Exception {

		if (!map.containsKey("mobile")) {
			return this.getError("mobile不能为空");
		}
		String mobile = map.get("mobile").toString();
		String msg = StringUtil.getRandomNum();

		final String myMsg = msg;
		final String myMobile = mobile;
		executor.execute(new Runnable() {
			@Override
			public void run() {
				DanskinSMSApiClient.createInstance().sendMessage(myMobile, "您的手机验证码为" + myMsg + "。请在2分钟内输入，如非本人操作请忽略。");
			}
		});

		JSONObject json = new JSONObject();
		json.put("mobile", mobile);
		json.put("validCode", msg);
		return new AccessResponse<Object>("0", "ok", json);
	}

	@RequestMapping(value = "getZhugeAppKey/{appId}", method = RequestMethod.POST)
	public Object getZhugeAppKey(@PathVariable("appId") String appId) throws Exception {

		String zhugeAppKey = "";
		CrmBrandInfo brandInfo = crmBrandInfoService.selectCrmBrandInfoByAppId(appId);
		if (brandInfo != null) {
			zhugeAppKey = brandInfo.getZhugeAppKey();
		}

		JSONObject json = new JSONObject();
		json.put("zhugeAppKey", zhugeAppKey);

		return new AccessResponse<Object>("0", "ok", json);
	}

	@RequestMapping(value = "activeMember/{appId}", method = RequestMethod.POST)
	public Object activeMember(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId)
			throws Exception {

		if (!map.containsKey("mobile")) {
			return this.getError("mobile不能为空");
		}
		if (!map.containsKey("openId")) {
			return this.getError("openId不能为空");
		}
		if (!map.containsKey("cardId")) {
			return this.getError("cardId不能为空");
		}
		if (!map.containsKey("userCardCode")) {
			return this.getError("userCardCode不能为空");
		}
		String mobile = map.get("mobile").toString();
		String openId = map.get("openId").toString();
		String cardId = map.get("cardId").toString();
		String userCardCode = map.get("userCardCode").toString();

		WxMediaPlatform entity = this.getMPWithAccessToken(appId);
		if (entity != null) {

			custInfoService.activateMemberInfo2(entity.getAccessToken(), entity.getId(), appId, mobile, openId, cardId,
					userCardCode);
		}

		JSONObject json = new JSONObject();
		json.put("mobile", mobile);
		json.put("openId", openId);

		return new AccessResponse<Object>("0", "ok", json);
	}

	@RequestMapping(value = "updateMember/{appId}", method = RequestMethod.POST)
	public Object updateMember(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId)
			throws Exception {

		if (!map.containsKey("mobile")) {
			return this.getError("mobile不能为空");
		}
		if (!map.containsKey("openId")) {
			return this.getError("openId不能为空");
		}
		String mobile = map.get("mobile").toString();
		String openId = map.get("openId").toString();

		WxMediaPlatform entity = this.getMPWithAccessToken(appId);
		if (entity != null) {

			custInfoService.updateMember(entity, openId, mobile);
		}

		JSONObject json = new JSONObject();
		json.put("mobile", mobile);
		json.put("openId", openId);

		return new AccessResponse<Object>("0", "ok", json);
	}
}
